<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="./src/vue.js"></script>
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .cates_title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 50px;
        border: 1px solid lightblue;
        padding: 0 10px;
      }
      .cates_content {
        display: flex;
        align-items: center;
        height: 150px;
      }
      .cates_content img {
        width: 123px;
        height: 92px;
      }
      .cates_content_info {
        height: 66%;
        padding: 20px 20px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
      }
      .cates_content_info p {
        color: gray;
      }
      .imgs {
        position: relative;
      }
      .imgs .tags {
        position: absolute;
        top: 0;
        left: 0;
        background-color: orange;
        color: white;
        width: 70px;
        height: 30px;
        text-align: center;
        line-height: 30px;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="cates">
        <cates_title title="图文教程"></cates_title>
        <div v-for="tuto in tutorials" :key="tuto.id">
          <cates_content :course="tuto"></cates_content>
        </div>
      </div>
      <div class="cates">
        <cates_title title="推荐资讯"></cates_title>
        <div v-for="n_obj in news" :key="n_obj.id">
          <cates_content :course="n_obj"></cates_content>
        </div>
      </div>
    </div>
    <template id="title">
      <div class="cates_title">
        <h3>{{title}}</h3>
        <p>查看更多&gt;</p>
      </div>
    </template>
    <template id="content">
      <div class="cates_content">
        <div class="imgs">
          <img :src="'https://ss.lanqb.com/' + course.imgCover" alt="" />
          <div class="tags" v-if="course.category">
            {{course.category.category_name}}
          </div>
        </div>
        <div class="cates_content_info">
          <h3>{{course.title}}</h3>
          <p v-if="course.user_profile">
            老师: {{course.user_profile.nickname}}
          </p>
          <p v-else>{{course.created_at}}</p>
        </div>
      </div>
    </template>
    <script>
      const cates_title = {
        template: "#title",
        props: ["title"],
      };
      const cates_content = {
        template: "#content",
        props: ["course"],
      };
      const vm = new Vue({
        el: "#app",
        data: {
          news: [],
          tutorials: [],
        },
        components: {
          cates_title,
          cates_content,
        },
        created() {
          // 推荐资讯
          fetch("https://m.lanqb.com/api/home/news")
            .then(res => res.json())
            .then(data => {
              this.news = data.slice(0, 3);
            });
          // 图文教程
          fetch("https://m.lanqb.com/api/home/graphic-tutorials")
            .then(res => res.json())
            .then(data => {
              this.tutorials = data.slice(0, 3);
            });
        },
      });
    </script>
  </body>
</html>
